import subprocess
import os


def execute_program(filename):
    """执行程序文件并捕获输出"""
    file_path = "./" + filename
    try:
        # 在Windows下可能需要调整为shell=True
        process = subprocess.Popen([file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=(os.name == 'nt'))
        stdout, stderr = process.communicate()

        # 检查进程是否成功执行（return code为0表示成功）
        if process.returncode == 0:
            return True, stdout.decode()
        else:
            return False, stderr.decode()
    except Exception as e:
        return False, str(e)
